package org.jboss.resteasy.springboot;

import java.util.Iterator;
import java.util.Set;
import javax.servlet.Servlet;
import org.jboss.resteasy.plugins.server.servlet.HttpServlet30Dispatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.ServletRegistrationBean;

public class ResteasyApplicationBuilder {
    public static final String BEAN_NAME = "JaxrsApplicationServletBuilder";
    private static final Logger logger = LoggerFactory.getLogger(ResteasyApplicationBuilder.class);

    public ResteasyApplicationBuilder() {
    }

    public ServletRegistrationBean build(String applicationClassName, String path, Set<Class<?>> resources, Set<Class<?>> providers) {
        Servlet servlet = new HttpServlet30Dispatcher();
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servlet, new String[0]);
        servletRegistrationBean.setName(applicationClassName);
        servletRegistrationBean.setLoadOnStartup(1);
        servletRegistrationBean.setAsyncSupported(true);
        servletRegistrationBean.addInitParameter("javax.ws.rs.Application", applicationClassName);
        if (path != null) {
            String mapping = path;
            if (!path.startsWith("/")) {
                mapping = "/" + path;
            }

            String prefix = mapping;
            if (!"/".equals(mapping) && mapping.endsWith("/")) {
                prefix = mapping.substring(0, mapping.length() - 1);
            }

            if (mapping.endsWith("/")) {
                mapping = mapping + "*";
            } else {
                mapping = mapping + "/*";
            }

            servletRegistrationBean.addInitParameter("resteasy.servlet.mapping.prefix", prefix);
            servletRegistrationBean.addUrlMappings(new String[]{mapping});
        }

        Iterator var9;
        Class provider;
        StringBuilder builder;
        boolean first;
        if (resources.size() > 0) {
            builder = new StringBuilder();
            first = true;

            for(var9 = resources.iterator(); var9.hasNext(); builder.append(provider.getName())) {
                provider = (Class)var9.next();
                if (first) {
                    first = false;
                } else {
                    builder.append(",");
                }
            }

            servletRegistrationBean.addInitParameter("resteasy.scanned.resources", builder.toString());
        }

        if (providers.size() > 0) {
            builder = new StringBuilder();
            first = true;

            for(var9 = providers.iterator(); var9.hasNext(); builder.append(provider.getName())) {
                provider = (Class)var9.next();
                if (first) {
                    first = false;
                } else {
                    builder.append(",");
                }
            }

            servletRegistrationBean.addInitParameter("resteasy.scanned.providers", builder.toString());
        }

        logger.debug("ServletRegistrationBean has just bean created for JAX-RS class " + applicationClassName);
        return servletRegistrationBean;
    }
}
